﻿using System;
using Microsoft.SharePoint.WebPartPages;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using System.Web.UI;
using System.Web.UI.HtmlControls;

namespace VPLink.WebParts
{
    public class FilterManagerToolPart : ToolPart
    {
        private HtmlSelect slcFilterField1;
        private HtmlSelect slcFilterField2;
        private HtmlSelect slcFilterType1;
        private HtmlSelect slcFilterType2;
        private HtmlInputText tbxFilterValue1;
        private HtmlInputText tbxFilterValue2;
        private RadioButtonList radFilterOperatorList;

        /// <summary>
        /// Constructor for the class.
        /// </summary>        
        public FilterManagerToolPart()
        {
            this.Title = "Outil de gestion des filtres des WebParts :";
            this.PreRender += new EventHandler(FilterManagerToolPart_PreRender);
        }

        /// <summary>
        /// Create dropdownlist to display lists
        /// </summary>
        override protected void CreateChildControls()
        {
            try
            {
                ListViewWebPart currentWebPart = (ListViewWebPart)ParentToolPane.SelectedWebPart;

                #region Filters

                Label lblFilter = new Label();
                lblFilter.Text = "Filtres :";
                Controls.Add(lblFilter);
                Controls.Add(new LiteralControl("<br/>&nbsp;&nbsp;&nbsp;"));

                // Filter Fields
                slcFilterField1 = new HtmlSelect();
                slcFilterField2 = new HtmlSelect();
                if (currentWebPart != null)
                {
                    Guid activeListID = currentWebPart.ListID;
                    Guid activeWebID = currentWebPart.WebID;

                    if (activeListID != Guid.Empty)
                    {
                        SPFieldCollection fc;
                        if (activeWebID != null && activeWebID != Guid.Empty)
                        {
                            using (SPWeb web = SPContext.Current.Site.OpenWeb(activeWebID))
                            {
                                fc = web.Lists[activeListID].Fields;
                            }
                        }
                        else
                            fc = SPContext.Current.Web.Lists[activeListID].Fields;

                        slcFilterField1.Items.Add(new ListItem("<aucun filtre>", string.Empty));
                        slcFilterField2.Items.Add(new ListItem("<aucun filtre>", string.Empty));
                        foreach (SPField f in fc)
                        {
                            if (f.Filterable && !f.Hidden)
                            {
                                slcFilterField1.Items.Add(new ListItem(f.Title, f.InternalName + ";" + f.TypeAsString));
                                slcFilterField2.Items.Add(new ListItem(f.Title, f.InternalName + ";" + f.TypeAsString));
                            }
                        }
                        slcFilterField1.SelectedIndex = 0;
                        slcFilterField1.Disabled = false;
                        slcFilterField2.SelectedIndex = 0;
                        slcFilterField2.Disabled = false;
                    }
                    else
                    {
                        slcFilterField1.Disabled = true;
                        slcFilterField2.Disabled = true;
                    }
                }
                slcFilterField1.Style.Add("width", "171px");
                slcFilterField2.Style.Add("width", "171px");
                Controls.Add(slcFilterField1);
                Controls.Add(new LiteralControl("<br/>&nbsp;&nbsp;&nbsp;"));

                // Filter type 1
                slcFilterType1 = new HtmlSelect();
                slcFilterType1.Items.Add(new ListItem(string.Empty, string.Empty));
                slcFilterType1.Items.Add(new ListItem("est égal à", "Eq"));
                slcFilterType1.Items.Add(new ListItem("n'est pas égal à", "Neq"));
                slcFilterType1.Items.Add(new ListItem("est supérieur à", "Gt"));
                slcFilterType1.Items.Add(new ListItem("est supérieur ou égal à", "Geq"));
                slcFilterType1.Items.Add(new ListItem("est inférieur à", "Lt"));
                slcFilterType1.Items.Add(new ListItem("est inférieur ou égal à", "Leq"));
                slcFilterType1.Items.Add(new ListItem("commence par", "BeginsWith"));
                slcFilterType1.Items.Add(new ListItem("contient", "Contains"));
                slcFilterType1.Style.Add("width", "171px");
                Controls.Add(slcFilterType1);
                Controls.Add(new LiteralControl("<br/>&nbsp;&nbsp;&nbsp;"));

                // Filter value 1
                tbxFilterValue1 = new HtmlInputText();
                tbxFilterValue1.Style.Add("width", "165px");
                Controls.Add(tbxFilterValue1);
                Controls.Add(new LiteralControl("<br/>&nbsp;&nbsp;&nbsp;&nbsp;"));

                radFilterOperatorList = new RadioButtonList();
                radFilterOperatorList.Items.Add(new ListItem("Et", "And"));
                radFilterOperatorList.Items.Add(new ListItem("Ou", "Or"));
                radFilterOperatorList.RepeatDirection = RepeatDirection.Horizontal;
                radFilterOperatorList.RepeatColumns = 2;
                radFilterOperatorList.RepeatLayout = RepeatLayout.Table;
                radFilterOperatorList.CellPadding = 5;
                Controls.Add(radFilterOperatorList);
                Controls.Add(new LiteralControl("<br/>&nbsp;&nbsp;&nbsp;"));

                Controls.Add(slcFilterField2);
                Controls.Add(new LiteralControl("<br/>&nbsp;&nbsp;&nbsp;"));

                // Filter type 2
                slcFilterType2 = new HtmlSelect();
                slcFilterType2.Items.Add(new ListItem(string.Empty, string.Empty));
                slcFilterType2.Items.Add(new ListItem("est égal à", "Eq"));
                slcFilterType2.Items.Add(new ListItem("n'est pas égal à", "Neq"));
                slcFilterType2.Items.Add(new ListItem("est supérieur à", "Gt"));
                slcFilterType2.Items.Add(new ListItem("est supérieur ou égal à", "Geq"));
                slcFilterType2.Items.Add(new ListItem("est inférieur à", "Lt"));
                slcFilterType2.Items.Add(new ListItem("est inférieur ou égal à", "Leq"));
                slcFilterType2.Items.Add(new ListItem("commence par", "BeginsWith"));
                slcFilterType2.Items.Add(new ListItem("contient", "Contains"));
                slcFilterType2.Style.Add("width", "171px");
                Controls.Add(slcFilterType2);
                Controls.Add(new LiteralControl("<br/>&nbsp;&nbsp;&nbsp;"));

                tbxFilterValue2 = new HtmlInputText();
                tbxFilterValue2.Style.Add("width", "165px");
                Controls.Add(tbxFilterValue2);

                #endregion
            }
            catch (Exception e)
            {
                Log.Write(e.Message + "\r\n" + e.StackTrace, System.Diagnostics.EventLogEntryType.Error);
            }
        }

        ///	<summary>
        ///	This method is called by the ToolPane object to apply property changes to the selected Web Part. 
        ///	</summary>
        override public void ApplyChanges()
        {
            // Apply property values here.
            ListViewWebPart currentWebPart = (ListViewWebPart)ParentToolPane.SelectedWebPart;
            if (currentWebPart != null)
            {
                string[] fieldAndType1 = slcFilterField1.Value.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                currentWebPart.FilterField1 = (slcFilterField1.SelectedIndex == 0) ? string.Empty : fieldAndType1[0];
                currentWebPart.FilterFieldType1 = (slcFilterField1.SelectedIndex == 0) ? string.Empty : fieldAndType1[1];

                currentWebPart.FilterType1 = (slcFilterType1.SelectedIndex == 0) ? string.Empty : slcFilterType1.Value;
                currentWebPart.FilterValue1 = string.IsNullOrEmpty(tbxFilterValue1.Value) ? string.Empty : tbxFilterValue1.Value;

                if (radFilterOperatorList.SelectedItem != null) currentWebPart.FilterOperator = radFilterOperatorList.SelectedItem.Value;

                string[] fieldAndType2 = slcFilterField2.Value.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                currentWebPart.FilterField2 = (slcFilterField2.SelectedIndex == 0) ? string.Empty : fieldAndType2[0];
                currentWebPart.FilterFieldType2 = (slcFilterField2.SelectedIndex == 0) ? string.Empty : fieldAndType2[1];

                currentWebPart.FilterType2 = (slcFilterType2.SelectedIndex == 0) ? string.Empty : slcFilterType2.Value;
                currentWebPart.FilterValue2 = string.IsNullOrEmpty(tbxFilterValue2.Value) ? string.Empty : tbxFilterValue2.Value;
            }
        }

        ///	<summary>
        ///	Initializes ToolPart values. 
        ///	</summary>
        private void FilterManagerToolPart_PreRender(object sender, EventArgs ea)
        {
            try
            {
                ListViewWebPart currentWebPart = (ListViewWebPart)ParentToolPane.SelectedWebPart;
                if (currentWebPart != null)
                {
                    foreach (ListItem item in slcFilterField1.Items)
                    {
                        item.Selected = (item.Value == currentWebPart.FilterField1 + ";" + currentWebPart.FilterFieldType1);
                    }
                    foreach (ListItem item in slcFilterType1.Items)
                    {
                        item.Selected = (item.Value == currentWebPart.FilterType1);
                    }
                    tbxFilterValue1.Value = currentWebPart.FilterValue1;

                    foreach (ListItem item in radFilterOperatorList.Items)
                    {
                        item.Selected = (item.Value == currentWebPart.FilterOperator);
                    }

                    foreach (ListItem item in slcFilterField2.Items)
                    {
                        item.Selected = (item.Value == currentWebPart.FilterField2 + ";" + currentWebPart.FilterFieldType2);
                    }
                    foreach (ListItem item in slcFilterType2.Items)
                    {
                        item.Selected = (item.Value == currentWebPart.FilterType2);
                    }
                    tbxFilterValue2.Value = currentWebPart.FilterValue2;
                }
            }
            catch (Exception e)
            {
                Log.Write(e.Message + "\r\n" + e.StackTrace, System.Diagnostics.EventLogEntryType.Error);
            }
        }
    }
}